* prepare data file for analysis
* calculates values, adds labels
* merges in fuel prices and other time varying variables

* location of datafiles
local trackdatafile = "${base_dir}/data/final/CA_tracks.csv" // updated to include NA ECA values, and Ent/Clear ports

local analysis_dta = "`1'"

* location of fuel price and cpi data
local price_file "${base_dir}/data_not_to_share/SPfuelprices/SPfuelprices.csv"
local cpi_file "${base_dir}/data/CPI/CPIAUCSL"
local unem_file "${base_dir}/data/unemployment/UNRATE"

* top and bottom text for tabout 
global top_tex = "${base_dir}/analysis_code/top_tabout.tex"
global bot_tex = "${base_dir}/analysis_code/bot_tabout.tex"


********************************************************************************
disp "`analysis_dta'"

* Assembling *******************************************************************
do "${base_dir}/analysis_code/CAtracks_assemble.do" ///
	"`trackdatafile'" "`price_file'" "`cpi_file'" "`unem_file'"
save "`analysis_dta'" , replace

* INITIAL CLEANING *************************************************************

* dropping LA to LB voyages										
drop if route_name=="LALB_LALB" 
										
*replace length=. if length>450 // clearing vessels that are too large
								// no longer necessary

* dropping unused vessel types
drop if inlist(group_agg,"Passenger","Miscel")
drop if inlist(group,"Tug","Dredge")

* dropping PW Sound and Cook Inlet routes (AK routes that cannot be properly interpolated)
drop if otherAK == 1 

* dropping HI to HI routes
drop if HItoHI == 1 

* if track crosses land too much then drop (more than 5km)
drop if km_onland>5

* drop if fuel consumption equals zero
drop if f_cons==0 | f_power==0

********************************************************************************
* recovering eca dates
levelsof eca_ind if eca_ind>0 , local(eca_inds)
foreach i of local eca_inds {
	qui sum date1 if eca_ind==`i'
	scalar eca`i'd = r(min)
	scalar eca`i'qy = yq( year(r(min)),quarter(r(min)) ) 
	scalar eca`i'my = ym( year(r(min)),month(r(min)) ) 
}

* Other Cleaning ***************************************************************
* creates main_sample variable for use later
do "${base_dir}/analysis_code/cleaning.do"

* predict FCC for vessels without characteristics
do "${base_dir}/analysis_code/predict_FCC.do"

* count number of tracks by route
egen tracks_by_route = sum(main_sample)  , by(route_id)

/*
* count of tracks by route in each policy period
* for making "balanced" panel
foreach i of num 0/5 {
	egen tmp_eca_count`i' = total(eca_ind==`i') , by(route_id)
}

gen balanced01 = tmp_eca_count0>0 & tmp_eca_count1>0
foreach i of num 2/5 {
	local pre = `i'-1
	display `pre'
	gen balanced0`i' = balanced0`pre' & tmp_eca_count`i'>0
}
drop tmp_*	
	
* count of tracks by route and vessel in each policy period
* for making "balanced" panel
foreach i of num 0/5 {
	egen tmp_eca_count`i' = total(eca_ind==`i') , by(route_id imo dirnw)
}

local bal_cut 2 // minimum number of observations to be in balanced sample
gen observed_pre_rte_ves = tmp_eca_count0>=`bal_cut'
gen balanced01_rte_ves = tmp_eca_count0>=`bal_cut' & tmp_eca_count1>0 if main_sample==1
foreach i of num 2/5 {
	local pre = `i'-1
	display `pre'
	gen balanced0`i'_rte_ves = balanced0`pre'_rte_ves & tmp_eca_count`i'>=`bal_cut' if main_sample==1
}
*tab balanced01_rte_ves if main_sample
*tab balanced02_rte_ves if main_sample

drop tmp_*		
*/
	
* flagging low count routes ****************************************************
**** average less than 1 per month in pre ECA period
**** doing with E/C routes and Boundary routes

local rte_vars route_id route_ec

foreach rte_var of local rte_vars {
	preserve
	egen tag_eca_mon = tag(my eca_ind)
	egen eca_mon = sum(tag_eca_mon) , by(eca_ind)
	collapse (count) main_sample (first) eca_mon if eca_ind<=2 , by(`rte_var' eca_ind vesseltype_reg)
	egen rte_ves = group(`rte_var' vesseltype_reg)
	drop if rte_ves==.
	gen cnt_mnth = main_sample/eca_mon
	reshape wide main_sample cnt_mnth eca_mon, i(rte_ves) j(eca_ind)
	gen low_cnt_rte_ves_`rte_var' = (cnt_mnth0<1)
	drop main_sample* eca_mon* cnt_mnth* rte_ves
	save low_counts , replace
	restore 

	merge m:1 `rte_var' vesseltype_reg using low_counts 
	drop _merge
}
rename low_cnt_rte_ves_route_id low_cnt_rte_ves
rename low_cnt_rte_ves_route_ec low_cnt_rte_ves_ec

* counting entrances/exits from a given port by month
egen ent_port_m = count(dist) , by(port2 my)
egen exit_port_m = count(dist) , by(port1 my)

* counts of vessels in ECA
gen in_eca09 = (dist_eca2009>0)
gen in_eca11 = (dist_eca2011>0)

* flag as super slow steaming
capture drop super_slow
gen super_slow = (d_km_below24/dist) > .75

* Labels ***********************************************************************
label variable eca_ind "Policy Treatment"
*label variable port_ifexit "Port"
label variable route_name "Route"
label variable vesseltype "Vessel Type"
label var USflag "US Flag"

label var dist "Dist, (km)"
label var dist_entexit "Dist, (km)"
label var f_cons "Fuel, (t)"
label var cost_comply_cons "Fuel Cost, (USD)"
label var td_comply_cons "Damage AP2, (USD)"
label var td_inm_comply_cons "Damage, (USD)"
label var avg_speed "Speed, (km/h)"
label var avg_speed_eca2009 "ECA Speed, (km/h)"
label var avg_speed_eca2011 "ECA Speed, (km/h)"
label var tkm_cons "Fuel, (t/km)"
label var tdtonne_pre_cons "Damage, (USD per t fuel)"
label var tdtonne_inm_pre_cons "Damage, (USD per t fuel)"
label var e_so2_comply_cons "SO2, (t)"
label var e_pm_comply_cons "PM, (t)"
label var e_so2_comply_cons_ineca09 "ECA SO2, (t)" 
label var e_pm_comply_cons_ineca09 "ECA PM, (t)" 
label var e_so2_comply_cons_ineca11 "ECA SO2, (t)" 
label var e_pm_comply_cons_ineca11 "ECA PM, (t)" 

label var td_inm_dist_eca09_cons "ECA Damages, Comply Pre, (USD)"
label var td_inm_res_out09_cons "Out Damages, (USD)"

label var cost_comply_cons_blend "Fuel Cost (1%), (USD)"
label var td_inm_comply_cons_blend "Damage (1%), (USD)"
label var cost_eca09_cons_blend "Fuel Cost (1%), Comply Pre, (USD)"
label var td_inm_eca09_cons_blend "Damage (1%), Comply Pre, (USD)"
label var cost_eca11_cons_blend "Fuel Cost (1%), Comply Pre, (USD)"
label var td_inm_eca11_cons_blend "Damage (1%), Comply Pre, (USD)"


/*
label var ltd_eca09_cons "Log(Damage, Comply)"
label var ltd_inm_eca09_cons "Log(Damage, Comply)"
label var lcost_eca09_cons "Log(Fuel Cost, Comply)"
label var ltd_eca11_cons "Log(Damage, Comply)"
label var ltd_inm_eca11_cons "Log(Damage, Comply)"
label var lcost_eca11_cons "Log(Fuel Cost, Comply)"
*/

label var td_eca09_cons "Damage AP2, Comply Pre, (USD)"
label var td_inm_eca09_cons "Damage, Comply Pre, (USD)"
label var td_inm_pre_cons "Damage, Resid, (USD)"
label var e_so2_eca09_cons "SO2, Comply Pre, (t)"
label var e_pm_eca09_cons "PM, Comply Pre, (t)"
label var e_pm_eca11_cons "PM, Comply Pre, (t)"
label var e_so2_dist_eca09_cons "ECA SO2, Comply Pre, (t)" 
label var e_pm_dist_eca09_cons "ECA PM, Comply Pre, (t)" 
label var e_pm_dist_eca11_cons "ECA PM, Comply Pre, (t)" 
label var cost_eca09_cons "Fuel Cost, Comply Pre, (USD)"
label var cost_pre_cons "Fuel Cost, Resid, (USD)"
label var td_eca11_cons "Damage AP2, Comply Pre, (USD)"
label var td_inm_eca11_cons "Damage, Comply Pre, (USD)"
label var cost_eca11_cons "Fuel Cost, Comply Pre, (USD)"

label var td_nox_cons "NOx Damage, (USD)"
label var td_nox_power "NOx Damage, (USD)"
label var tdtonne_nox_cons "NOx Damage, (USD/t)"

label var td_voc_cons "VOC Damage, (USD)"
label var td_voc_power "VOC Damage, (USD)"
label var tdtonne_voc_cons "VOC Damage, (USD/t)"

label var td_corr_cons "NOx+VOC, Damage, (USD)"
*label var td_corr_power "NOx+VOC Damage, (USD)"
label var tdtonne_corr_cons "NOx+VOC, Damage, (USD/t)"

label var dist_eca2009 "ECA Dist, (km)"
label var f_eca09_cons "ECA Fuel, (t)"
label var tkm_eca09_cons "ECA Fuel, (t/km)"
label var tkm_out09_cons "Out Fuel, (t/km)"

label var dist_eca2011 "ECA Dist, (km)"
label var f_eca11_cons "ECA Fuel, (t)"
label var tkm_eca11_cons "ECA Fuel, (t/km)"
label var tkm_out11_cons "Out Fuel, (t/km)"

label var built "Built (y)"
label var loa "Length (m)"
label var dwt "DWT (t)"
label var beam "Beam (m)"
label var draft "Draft"
label var power_kw "Power (kw)"

label var hours "Travel Time, (h)"


label var in_eca09 "2009 ECA"
label var in_eca11 "2011 ECA"
label var dist_eca2009 "2009 ECA"
label var dist_eca2011 "2011 ECA"
label var f_eca09_cons "2009 ECA"
label var f_eca11_cons "2011 ECA"

*** Resaving *******************
save "`analysis_dta'" , replace

